package com.zhuoyue.biz.core.api.service.impl.device;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.dao.TblDeviceTaskMapper;
import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.domain.TblDeviceTask;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.api.service.DeviceTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zhanghui
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DeviceTaskCommon {
    private final RedisTemplate redisTemplate;

    private final TblDeviceTaskMapper taskMapper;

    private final DeviceTaskService taskService;
    private static final String DELETE_TIMED_TASK ="DeleteTimedTask";


    public void saveToRedis(ControlReq controlReq, String cmd, int expire) {
        Map<String, Object> actions = controlReq.getActions();

        //控制类型
        int controlType = Integer.parseInt(actions.get("controlType").toString());

        //红外设备MAC
        String zigbeeMac = cmd.substring(24, 40);

        //定时使能
        int status = Integer.parseInt(actions.get("taskEnable").toString());

        //任务序号
        int taskIndex = Integer.parseInt(actions.get("taskIndex").toString());

        //重发次数
        int repeatTime = Integer.parseInt(actions.get("repeatTime").toString());

        //重发间隔
        int taskInterval = Integer.parseInt(actions.get("taskInterval").toString());

        //定时时间
        String startTime = controlReq.getActions().get("startTime").toString();
        //若传入的小时与分钟没有0前缀则添加
        String hour = startTime.substring(0, startTime.indexOf(':'));
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        String minute = startTime.substring(startTime.indexOf(':') + 1);
        if (minute.length() == 1) {
            minute = "0" + minute;
        }
        startTime = hour + ":" + minute;

        //控制参数
        JSONObject controlParams = new JSONObject();
        controlParams.put("acSwitch", actions.get("acSwitch"));
        controlParams.put("acMode", actions.get("acMode"));
        controlParams.put("acWindSpeed", actions.get("acWindSpeed"));
        controlParams.put("acWindDirection", actions.get("acWindDirection"));
        controlParams.put("acTemp", actions.get("acTemp"));


        TblDeviceTask deviceTask = new TblDeviceTask();
        deviceTask.setZigbeeMac(zigbeeMac);
        deviceTask.setTaskIndex(taskIndex);
        deviceTask.setStatus(status);
        deviceTask.setRepeatTime(repeatTime);
        deviceTask.setTaskInterval(taskInterval);
        deviceTask.setStartTime(startTime);
        deviceTask.setControlType(controlType);
        deviceTask.setControlParams(controlParams.toJSONString());

        String key = "set_device_task_zigbeeMac_" + deviceTask.getZigbeeMac() + "_index_" + taskIndex;

        Map<String, Object> information = new HashMap(2);
        information.put("controlType", controlReq.getControlType());
        information.put("deviceTask", deviceTask);
        //将对象保存到redis
        redisTemplate.opsForValue().set(key, information, expire, TimeUnit.SECONDS);
    }

    public ControlReq getControlParams(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String zigbeeMac = deviceAndGatewayDO.getZigbeeMac();
        String controlType = controlReq.getControlType();

        int taskIndex = Integer.parseInt(controlReq.getActions().get("taskIndex").toString());
        TblDeviceTask deviceTask = taskMapper.selectOne(Wrappers.<TblDeviceTask>lambdaQuery().eq(TblDeviceTask::getZigbeeMac, zigbeeMac).eq(TblDeviceTask::getTaskIndex, taskIndex));
        if (deviceTask == null) {
            return controlReq;
        }

        JSONObject controlParams = JSONObject.parseObject(deviceTask.getControlParams());

        Map<String, Object> actions = controlReq.getActions();
        //若是刪除，则状态设置为不可用
        if (DELETE_TIMED_TASK.equals(controlType)) {
            actions.put("taskEnable", 0);
        }
        actions.put("startTime", deviceTask.getStartTime());
        actions.put("taskInterval", deviceTask.getTaskInterval());
        actions.put("repeatTime", deviceTask.getRepeatTime());
        actions.put("controlType", deviceTask.getControlType());
        actions.put("acSwitch", controlParams.getString("acSwitch"));
        actions.put("acMode", controlParams.getString("acMode"));
        actions.put("acWindSpeed", controlParams.getString("acWindSpeed"));
        actions.put("acWindDirection", controlParams.getString("acWindDirection"));
        actions.put("acTemp", controlParams.get("acTemp"));
        return controlReq;
    }
}
